function varargout = interface1(varargin)
% INTERFACE1 M-file for interface1.fig
%      INTERFACE1, by itself, creates a new INTERFACE1 or raises the existing
%      singleton*.
%
%      H = INTERFACE1 returns the handle to a new INTERFACE1 or the handle to
%      the existing singleton*.
%
%      INTERFACE1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in INTERFACE1.M with the given input arguments.
%
%      INTERFACE1('Property','Value',...) creates a new INTERFACE1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before interface1_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to interface1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help interface1

% Last Modified by GUIDE v2.5 06-Feb-2012 10:51:48

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @interface1_OpeningFcn, ...
                   'gui_OutputFcn',  @interface1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before interface1 is made visible.
function interface1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to interface1 (see VARARGIN)

% Choose default command line output for interface1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes interface1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = interface1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

updateUnits(1);
loadEngines();

function txtCrew_Callback(hObject, eventdata, handles)
% hObject    handle to txtCrew (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtCrew as text
%        str2double(get(hObject,'String')) returns contents of txtCrew as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtCrew_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtCrew (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtPax_Callback(hObject, eventdata, handles)
% hObject    handle to txtPax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtPax as text
%        str2double(get(hObject,'String')) returns contents of txtPax as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtPax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtPax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtLoad_Callback(hObject, eventdata, handles)
% hObject    handle to txtLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtLoad as text
%        str2double(get(hObject,'String')) returns contents of txtLoad as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtLoad_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtLoad (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtRange_Callback(hObject, eventdata, handles)
% hObject    handle to txtRange (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtRange as text
%        str2double(get(hObject,'String')) returns contents of txtRange as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtRange_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtRange (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtCruisesSpeed_Callback(hObject, eventdata, handles)
% hObject    handle to txtCruisesSpeed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtCruisesSpeed as text
%        str2double(get(hObject,'String')) returns contents of txtCruisesSpeed as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtCruisesSpeed_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtCruisesSpeed (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on selection change in cmbMaterial.
function cmbMaterial_Callback(hObject, eventdata, handles)
% hObject    handle to cmbMaterial (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns cmbMaterial contents as cell array
%        contents{get(hObject,'Value')} returns selected item from cmbMaterial


% --- Executes during object creation, after setting all properties.
function cmbMaterial_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cmbMaterial (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtLDMax_Callback(hObject, eventdata, handles)
% hObject    handle to txtLDMax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtLDMax as text
%        str2double(get(hObject,'String')) returns contents of txtLDMax as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtLDMax_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtLDMax (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtLDCruise_Callback(hObject, eventdata, handles)
% hObject    handle to txtLDCruise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtLDCruise as text
%        str2double(get(hObject,'String')) returns contents of txtLDCruise as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtLDCruise_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtLDCruise (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function txtLoiterDuration_Callback(hObject, eventdata, handles)
% hObject    handle to txtLoiterDuration (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of txtLoiterDuration as text
%        str2double(get(hObject,'String')) returns contents of txtLoiterDuration as a double
validateNumericField(hObject);

% --- Executes during object creation, after setting all properties.
function txtLoiterDuration_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txtLoiterDuration (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% Function that updates Metric Units on interface
function updateUnits(system)

    if system == 1
        set(findobj('Tag', 'uniWeight3'), 'String', 'kg');
        set(findobj('Tag', 'uniDistance1'), 'String', 'km');
        set(findobj('Tag', 'uniVel1'), 'String', 'km/h');
        set(findobj('Tag', 'uniTime'), 'String', 'h');
    elseif system == 2
        set(findobj('Tag', 'uniWeight3'), 'String', 'lb');
        set(findobj('Tag', 'uniDistance1'), 'String', 'nm');
        set(findobj('Tag', 'uniVel1'), 'String', 'ktas');
        set(findobj('Tag', 'uniTime'), 'String', 'h');
    end

% --- Executes on button press in rdbIS.
function rdbIS_Callback(hObject, eventdata, handles)
% hObject    handle to rdbIS (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of rdbIS
updateUnits(1);


% --- Executes on button press in rdbBU.
function rdbBU_Callback(hObject, eventdata, handles)
% hObject    handle to rdbBU (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of rdbBU
updateUnits(2);



% --- Executes on selection change in cmbEngine.
function cmbEngine_Callback(hObject, eventdata, handles)
% hObject    handle to cmbEngine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns cmbEngine contents as cell array
%        contents{get(hObject,'Value')} returns selected item from cmbEngine


% --- Executes during object creation, after setting all properties.
function cmbEngine_CreateFcn(hObject, eventdata, handles)
% hObject    handle to cmbEngine (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

%Function that loads the engines
function loadEngines()
    dbEngines;
    for i = 1:1:size(engines, 2)
        values(i) = {engines(i).name};
    end
set(findobj('Tag', 'cmbEngine'), 'String', values);

%Function that validates a numeric field
function validateNumericField(hObject)
inData = str2double(get(hObject, 'string'));
if isnan(inData)
    uicontrol(hObject)
    errordlg('You must enter a numeric value!', 'Bad input', 'modal')
end